package com.qst.control;

import com.qst.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }

    /**
     * 处理post请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//调用业务逻辑处理层
        LoginService loginService=new LoginService();
//        获取用户传递进来的用户名与密码
        String username=req.getParameter("username");
        String userpwd=req.getParameter("userpwd");
//        调用方法与数据库中的信息进行对比
        boolean ok=loginService.login(username,userpwd);
//        结果为true
        if(ok){
//            转发方式跳转到欢迎页
            req.getRequestDispatcher("welcome.jsp").forward(req,resp);
        }else {
//            结果为false 重定向到登录页面
            resp.sendRedirect("/index.jsp");
        }

    }
}
